package com.shenwei.yingdaoguanli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenwei.yingdaoguanli.entity.BackupLog;
import com.shenwei.yingdaoguanli.entity.FileCleanLog;
import com.shenwei.yingdaoguanli.entity.dto.BackupLogDto;
import com.shenwei.yingdaoguanli.mapper.BackupLogMapper;
import com.shenwei.yingdaoguanli.mapper.FileCleanLogMapper;
import com.shenwei.yingdaoguanli.service.BackupLogService;
import com.shenwei.yingdaoguanli.service.FileCleanLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;


/**
 * @Author wang
 * @Date 2025/1/8 22:45
 * @PackageName:com.shenwei.yingdaoguanli.service.impl
 * @ClassName: FileStatServiceImpl
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class BackupLogServiceImpl implements BackupLogService {

    @Autowired
    private BackupLogMapper backupLogMapper;

    @Override
    public Boolean deleteAll(String directoryPath) {
        Path dir = Paths.get(directoryPath);

        // 检查目录是否存在
        if (!Files.exists(dir) || !Files.isDirectory(dir)) {
            System.err.println("指定的路径不是有效的目录: " + directoryPath);
            return false;
        }

        try (Stream<Path> files = Files.list(dir)) {
            files.filter(path -> !Files.isDirectory(path))  // 过滤掉子目录
                    .filter(path -> path.toString().endsWith(".zip"))  // 筛选 .zip 文件
                    .forEach(path -> {
                        try {
                            Files.delete(path); // 删除文件
                            System.out.println("已删除文件: " + path);
                        } catch (IOException e) {
                            System.err.println("无法删除文件: " + path + " - " + e.getMessage());
                        }
                    });
            return true;
        } catch (IOException e) {
            System.err.println("遍历目录时发生错误: " + e.getMessage());
            return false;
        }
    }

    @Override
    public void save(BackupLog backupLog) {
        backupLogMapper.insert(backupLog);
    }

    @Override
    public List<BackupLog> list(BackupLogDto bakupLogDto) {
        return backupLogMapper.selectByDate(bakupLogDto);
    }



}